//
// Created by zggua on 2025/7/16.
//

#include "PingTools.h"
#include <QProcess>

PingTools::PingTools(QObject *parent): QObject(parent) {
}

PingTools::~PingTools() = default;

bool PingTools::Ping(const QString &ip, int timeout) {
    QProcess process;
    process.start();
    process.waitForFinished(timeout);
    if (process.exitCode() != 0) {
        return false;
    }
    process.start("ping", QStringList() << ip << "-n" << "1");
    process.waitForFinished(timeout);
    if (process.exitCode() != 0) {
        return false;
    }
    QString output = process.readAllStandardOutput();
    if (!output.contains("TTL=")) {
        return false;
    }
    return true;
}
